##### My (demuredemeanor) bashrc sub source function script
# Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
# https://notabug.org/demure/dotfiles/
# legacy repo http://github.com/demure/dotfiles
# vim:set syntax=sh:

### Extract Function ### {{{
## Extract most types of compressed files
function extract {
    echo Extracting $1 ...
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)  tar xjf $1      ;;
            *.tar.gz)   tar xzf $1      ;;
            *.bz2)      bunzip2 $1      ;;
            *.rar)      rar x $1        ;;
            *.gz)       gunzip $1       ;;
            *.tar)      tar xf $1       ;;
            *.tbz2)     tar xjf $1      ;;
            *.tgz)      tar xzf $1      ;;
            *.zip)      unzip $1        ;;
            *.Z)        uncompress $1   ;;
            *.7z)       7z x $1         ;;
            *.xz)       xz -d $1        ;; 
            *)          echo "'$1' cannot be extracted via extract()" ;;
        esac
        else
        echo "'$1' is not a valid file"
    fi
}
### End Extract ### }}}

### Man Function ### {{{
## Color man pages with less (is a `most` way too...)
man() {
    env \
    LESS_TERMCAP_mb=$(printf "\e[1;31m") \
    LESS_TERMCAP_md=$(printf "\e[1;31m") \
    LESS_TERMCAP_me=$(printf "\e[0m") \
    LESS_TERMCAP_se=$(printf "\e[0m") \
    LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
    LESS_TERMCAP_ue=$(printf "\e[0m") \
    LESS_TERMCAP_us=$(printf "\e[1;32m") \
    man "$@"
}
### End Man ### }}}

### Git Files Hist Function ### {{{
function git_files_hist() {
git log --pretty=%h | while read hash; do git ls-tree -r ${hash} | awk '{print $4}'; done | sort -u
}
### End Git Hist ### }}}

### Backup File ### {{{
# http://xmodulo.com/useful-bash-aliases-functions.html
backup() { cp "$1"{,.bak};}
### End Backup ### }}}

### IP Look Up ### {{{
ipinfo() {
    local IP1 IP2
    IP1="$1"
    IP2="$(echo "${IP1}" | grep -Ec '^(([0-9a-f]{0,4}:){1,7}[0-9a-f]{1,4}|([0-9]{1,3}\.){3}[0-9]{1,3})$')"
    if [[ ${IP2} == "1" ]]; then
        curl ipinfo.io/"${IP1}"
        else
        ipawk=($(host "${IP1}" | awk '/address/ { print $NF }'))
        curl ipinfo.io/${ipawk[1]}
    fi
    echo
}
### End IP Look Up ### }}}

### Mpv Search Youtube ### {{{
## mm "search terms"
function mm() {
    mpv --no-video --ytdl-format=bestaudio ytdl://ytsearch10:"$@"
}
### End Mpv Search ### }}}
